<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="0px" class="login-form">
      <h2 class="login-title">登录</h2>
      <el-form-item prop="no">
        <el-input v-model="loginForm.no" placeholder="账号" prefix-icon="el-icon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')" :loading="loading">登录</el-button>
        <el-button @click="resetForm('loginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        no: '',
        password: ''
      },
      rules: {
        no: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      },
      loading: false,
      message: this.$message,
      menu: [],
      roleId: ''
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$axios.post(this.$httpUrl + '/user/login', this.loginForm)
              .then(res => res.data)
              .then(res => {
                this.loading = false;
                if (res.code === 200) {
                  sessionStorage.setItem('CurUser', JSON.stringify(res.data));
                  this.roleId = res.data.roleId;
                  this.fetchMenuData(this.roleId); // 获取菜单数据
                  this.$router.push('/IndexPage');
                } else {
                  this.message.error(res.message || '登录失败');
                }
              })
              .catch(error => {
                this.loading = false;
                console.error('登录失败:', error);
                this.message.error('登录失败: ' + (error.response ? error.response.data.message : error.message));
              });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    fetchMenuData(roleId) {
      this.$axios.post(this.$httpUrl + '/menu/list', roleId,{
        headers: {
          'Content-Type': 'application/json'
        }
      })
          .then(res => res.data)
          .then(res => {
            if (res.code === 200) {
              this.menu = res.data.map(item => ({
                menuClick: item.menuclick,
                menuName: item.menuname,
                menuIcon: item.menuicon
              }));
              // 将菜单数据存储在 sessionStorage 中
              sessionStorage.setItem('menu', JSON.stringify(this.menu));
            } else {
              this.message.error(res.message || '获取菜单失败');
            }
          })
          .catch(error => {
            console.error('获取菜单失败:', error);
            this.message.error('获取菜单失败: ' + (error.response ? error.response.data.message : error.message));
          });
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-form {
  width: 300px;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}
</style>
